//useAdminQueryStore.ts
import { create } from "zustand";
import { useQuery } from "react-query";
import { QueryStore } from "../../../type";
import { useMemo } from "react";

export const adminBaseUrl = "http://localhost:4000/clothing/admin";

export const useQueryStore = create<QueryStore>(() => ({
  products: [],
}));

export function useAdminProducts() {
  const { data, isLoading, isError, isFetching } = useQuery(
    "products",
    async () => {
      const response = await fetch(adminBaseUrl);
      if (!response.ok) {
        throw new Error("Network response was not ok");
      }
      const data = await response.json();
      return data;
    }
  );

  const adminProducts = useMemo(() => data || [], [data]);

  return { adminProducts, isLoading, isError, isFetching };
}
